*******************************************************************************
* this do file is for: 1) testing IV validity using Mourifie and Wan (2017);
* data used in this file: kh_2009pa_cleaned.dta
* date: July 7, 2021
*******************************************************************************

* log file
log using "C:\Users\arthu\Desktop\merge1_1\kh_2009pa_test_iv_validity.log"

* global command
global data "C:\Users\arthu\Dropbox\research\mte_at\codes\replication\data"
global merge1_1 "C:\Users\arthu\Desktop\merge1_1"

***********************************************
* prepare: read data, set memory, gen variables
***********************************************

* read data
use "$data\kh_2009pa_cleaned.dta", clear

* set larger matrix size
set matsize 11000

* global outcome variable
global outcome "lenin east_ger poli_pow"

* generate c_d
egen c1 = mean(iv)
gen c0 = 1 - c1
disp c1

* gen: two moment inequalities in Mourifie and Wan (2017)
gen ybar1 = c1 * treatment * (1 - iv) - c0 * treatment * iv
gen ybar2 = c0 * (1 - treatment) * iv - c1 * (1 - treatment) * (1 - iv)
  
***************************************
* test IV validity for the whole sample
***************************************

foreach i in $outcome{

  * gen: grid points
  centile(`i'), centile(2.5 97.5) // stores the results in the return memory.
  scalar ub = r(c_2) // a scalar = 95th percentile of the data.
  scalar lb = r(c_1) // a scalar = 5th percentile of the data.
  gen grid = lb + 0.01 * _n
  replace grid = . if grid > ub
  
  * clr test
  clrtest (ybar1 `i' grid) (ybar2 `i' grid), level(0.9, 0.95, 0.99)
  
  * drop grid
  drop grid
  
}

****************
* close log file
****************

log close
